{{ define "/admin/purview/index" }}

{{ template "public/header" }}

<link rel="stylesheet" href="/static/admin/css/font.css">
<link rel="stylesheet" href="/static/lib/layui/css/eleTree.css" media="all">
<link rel="stylesheet" href="/static/lib/ztree/css/metroStyle/metroStyle.css" type="text/css">
<script type="text/javascript" src="/static/lib/ztree/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="/static/lib/ztree/js/jquery.ztree.excheck.js"></script>

<body>
<div class="x-nav">
          <span class="layui-breadcrumb">
          </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">
                    <button class="layui-btn" onclick="xadmin.open('添加管理权限','/admin/purview/add',800,600)"><i class="layui-icon"></i>添加</button>
                </div>
                <div class="layui-card-body ">
                    <ul id="purviewTree" class="ztree"></ul>
                </div>
            </div>
        </div>
    </div>
</div>
</body>

<script type="text/html" id="rowBar">
    <button class="layui-btn layui-btn-xs" lay-event="rowBarEdit">编辑</button>
    <button class="layui-btn layui-btn-xs layui-btn-danger" lay-event="rowBarDel">删除</button>
</script>

<script type="text/html" id="customBar">
    <a class="layui-btn layui-btn-xs" lay-event="customBarAdd">添加</a>
</script>

<script>
    var zTreeObj;
    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
    var setting = {
        view: {
            showIcon: false,
            addDiyDom: addDiyDom,
        },
        data: {
            simpleData: {
                enable: true
            }
        },
    };

    var data = '{{ .list }}'
    data = eval('(' + data + ')');

    $(document).ready(function () {
        zTreeObj = $.fn.zTree.init($("#purviewTree"), setting, data);
        zTreeObj.expandAll(true)
    });

    function addDiyDom(treeId, treeNode) {
        var aObj = $("#" + treeNode.tId + "_a");
        var editStr = "<i purviewId='" + treeNode.id + "' id='diyBtn1_" + treeNode.id + "' style='margin-left:15px;' class=\"layui-icon my-edit\">&#xe642;</i>";
        aObj.append(editStr);
        var btn1 = $("#diyBtn1_" + treeNode.id);
        if (btn1) btn1.bind("click", edit);

        var deleteStr = "<i purviewId='" + treeNode.id + "' id='diyBtn2_" + treeNode.id + "' class=\"layui-icon\" style='margin-left: 5px;'>&#xe640;</i>";
        aObj.append(deleteStr);
        var btn2 = $("#diyBtn2_" + treeNode.id);
        if (btn2) btn2.bind("click", del);
    };

    function edit() {
        var purviewId = $(this).attr('purviewId');
        xadmin.open('编辑', '/admin/purview/edit?id=' + purviewId)
    }

    function del() {
        var obj = $(this)
        layer.confirm('确认要删除吗？', function (index) {
            var purviewId = $(obj).attr('purviewId');
            var url = "/admin/purview/del";
            var data = {'id': purviewId};
            $.ajax({
                type: "post",
                url: url,
                data: data,
                dataType: "json",
                success: function (res) {
                    if (res.code == 0) {
                        layer.msg(res.msg, {
                            icon: 1,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        }, function () {
                            location.reload();
                        });
                    } else {
                        layer.msg(res.msg, {
                            icon: 2,
                            time: 3000//2秒关闭（如果不配置，默认是3秒）
                        });
                    }
                }
            });
        })
    }
</script>
</html>
{{ end }}